<template>
	<view :class="currentTheme">
		<a-nabbar name="购物车" />
		<mescroll-body ref="mescrollRef" @init="mescrollInit" :down="downOption" @up="upCallback" bottom="150" :up="upOption">
			<view class="u-padding-left-32 u-padding-right-32">
				<view class="u-padding-top-20 u-text-center">
					左滑移除购物车
				</view>
				<view class="u-margin-top-20 data-list" v-for="(item,index) in dataList" :key="item.id">
					<u-swipe-action :show="item.show"
					bg-color="whtie"
					btn-width="120"
						@click="click" @open="open" :index="index" 
						:options="options">
						<list-item :item="item" :showStar="false" :isShare="false"  width="158" height="158" :showTime="false">
							<view class="u-flex" style="width: 76rpx;"  @click.stop="">
								<u-checkbox v-model="item.checked" shape="circle" :active-color="currentThemeObj.primary" @change="getCheck"></u-checkbox>
							</view>
							<view v-if="item.type==='1111'" class="double-eleven u-position-absolute" slot="imageFixed">
								<item-image img="https://larp-gbo.oss-cn-chengdu.aliyuncs.com/wx/1111tag.png" :width="60" :height="30"></item-image>
							</view>
							<!-- <view class=""   @click.stop="">
								<u-icon name="close" class="close-icon u-position-absolute text-color-765CFE" size="36" @click="removeCart(item)"></u-icon>
							</view> -->
							<view slot="content" class="u-font-20 text-order-time u-margin-top-24" style="width: 100%;height: 2vh;">
								<text v-if="item.type=='script'">库存：{{item.productStock || 0}}</text>
							</view>
							<view class="u-flex u-row-between u-flex-1" slot="footer">
								<money size="36" :price="item.exhibitPrice" :color="currentThemeObj.primary"></money>
								<view  @click.stop="">
									<number-box v-model="item.cartNum" :min="0" :num="item.cartNum" @change="onChange"  :index="index" :max="item.productStock"  :input-width="80"  class="custom-u-number-box" :bgColor="currentThemeObj.cardbgColor" color="#765CFE" :disabledInput="!!item.experienceProductId" :disabled="!!item.experienceProductId"></number-box>
								</view>
								<!-- <view v-else>
										×{{item.cartNum}}
								</view> -->
							</view>
						</list-item>
					</u-swipe-action>
					
				</view>
			</view>
		</mescroll-body>
		<fixed-team>
			<view class="u-flex u-flex-1">
				<view class="">
					<u-checkbox v-model="checkedAll" shape="circle" :active-color="currentThemeObj.primary"  @change="checkedAllFn"><text class="box-title u-font-28">全选</text></u-checkbox>
				</view>
				<view class="u-flex u-flex-1 u-row-right">
					<view class="u-text-right u-margin-right-20 u-flex">
						<text class="u-font-32 box-title text-bold">合计：</text>
						<money :price="price" size="40" :color="currentThemeObj.primary"></money>
						<!-- <text class="u-font-34 text-color-765CFE">￥{{price}}</text> -->
					</view>
					<view class="">
						<item-btn @click="submit" width="206" height="73">提交订单</item-btn>
					</view>
				</view>
			</view>
		</fixed-team>
	</view>
</template>

<script>
	import {update,deleteIds} from '@/api/crud.js'
	import  settlement from '@/mixins/settlement.js'
	import upCallback from '@/mixins/upCallbackMixin.js'
	import { BigNumber } from "bignumber.js";
	import numberBox from '@/components/numberBox/numberBox'
	export default {
		mixins:[upCallback,settlement],
		components: {
			numberBox
		},
		data() {
			return {
				downOption: {
					use: false
				},
				upOption:{
					empty:{
						tip:'购物车里空空如也'
					}
				},
				form:{},
				dataList:[],
				apiModule:'shopApi',
				apiType:'cart',
				checkedAll:true,
				imgUrl:'',
				price:0,
				options:[{
					text: '删除',
					style: {
						backgroundColor: '#F03F3D'
					}
				}],
				isExperience:false
			};
		},
		methods:{
			// 如果打开一个的时候，不需要关闭其他，则无需实现本方法
			open(index) {
				// 先将正在被操作的swipeAction标记为打开状态，否则由于props的特性限制，
				// 原本为'false'，再次设置为'false'会无效
				this.dataList[index].show = true;
				this.dataList.forEach((val, idx) => {
					if(index != idx) this.dataList[idx].show = false;
				})
			},
			totalPrice(list){
				const initialValue = 0;
				const price = list.reduce((total,next)=>{
					let {exhibitPrice,cartNum} = next;
					exhibitPrice = BigNumber(exhibitPrice);
					const plus = exhibitPrice.multipliedBy(cartNum).plus(total)
					return plus.toNumber();
				},initialValue)
				//this.totalPriceNum +=price
				return price  || 0
			},
			getCheck(){
				setTimeout(()=>{
					const initialValue = 0;
					const list = this.dataList.filter(val=>val.checked);
					this.price = this.totalPrice(list);
					if(this.dataList.length>0){
						this.checkedAll = !this.dataList.some(val=>!val.checked)
					}else{
						this.checkedAll = false;
					}
				},0)
			},
			dataListCallBack(){
				this.dataList = this.dataList.map(val=>{
					this.$set(val,'checked',true)
					return val;
				})
				this.getCheck()
			},
			onChange({value,index}){
				const item = this.dataList[index];
				if(value>item.productStock){
					this.$u.toast('超出库存数量')
					return false;
				}
				if(value===0){
					this.$u.debounce(()=>this.removeCart(item,'onchange'),200) //查看uview源码得知 触发emit事件延迟了150ms 所以我们这里延迟200ms已达到同步 删除延迟会造成定时器错误
				}else{
					this.$u.debounce(()=>this.updataItem(item),350)
				}
			},
			updataItem(item){
				if(item.cartNum>item.productStock){
					this.$u.toast('超出库存数量')
					return false;
				}
				update(item,this.apiType,this.apiModule).then(this.getCheck)
			},
			submit(){
				if(this.price===0){
					this.$u.toast('结算金额为0')
					return false;
				}
				const checked = this.dataList.filter(val=>val.checked).map(val=>({productNum:val.cartNum,productId:val.productId,mallProductId:val.mallProductId,experienceProductId:val.experienceProductId,cartId:val.id}));
				
				//查看已选择的列表里有无体验本  
				let ifNotCanBuy = checked.find(val=>{
					return val.experienceProductId
				})
				//如果有 并且超过了三次  就不允许购买
				if(ifNotCanBuy && this.loginForm.notAdoptNum>=3){
					uni.showModal({
						showCancel:false,
						title:'温馨提示',
						content:'您填写的剧本体验退还原因未被采纳已累计次数达到3次，体验剧本功能已关闭。',
						success:()=> {}
					})
				}else{
					this.settlement(checked,'settlement')
				}
			},
			//移除购物车
			removeCart(item,type) {
				uni.showModal({
					cancelText:'手滑点错',
					confirmText:'狠心移出',
					title:'温馨提示',
					content:'确定将该商品移出购物车？',
					success: ({confirm,cancel}) => {
						confirm&&deleteIds('',this.apiType,this.apiModule,item.id).then(res=>{
							this.dataList = this.dataList.filter(val=>val.id!=item.id)
							!this.dataList.length&&this.mescroll.resetUpScroll()
							this.$u.toast('删除成功')
							this.getCheck()
						})
						if(cancel&&type==='onchange'){
							const cartNum = item.cartNum>0 ? item.cartNum : 1;
							this.$u.debounce(()=>this.$set(item,'cartNum',cartNum),350)
							
						}
					}
				})
			},
			//点击置顶或者删除按钮
			click(index, index1) {
				setTimeout(()=>{
					this.dataList = this.dataList.map((val, idx) => {
						this.$set(val,'show',false)
						return val;
					})
				},100)
				const item = this.dataList[index];
				this.removeCart(item)
			},
			getMinus(item,e){
				console.log(item)
			},
			checkedAllFn({value}){
				this.dataList = this.dataList.map(val=>{
					val.checked = value;
					return val;
				})
				this.getCheck()
			}
		}
	}
</script>

<style lang="scss">


.close-icon{
	right: 20rpx;
	top: 20rpx;
}
.u-checkbox__icon-wrap{
	border-color: var(--theme-color);
}
@mixin border {
	border: 1px solid #b5b5b5;
}
.bottom-submit{
	// border-top: 1rpx solid transparentize($color: #BFBFBF, $amount: 0.4);
	left: 0;
	right: 0;
	bottom: 0;
	padding-left: 64rpx;
	padding-right: 13rpx;
	background-color: var(--background-three-color);
	line-height: 100rpx;
	.price-tips{
		color:var(--title-color);
		font-size: 32rpx;
	}
}
.data-list /deep/ {
	.u-col-top{
		align-items: center;
	}
	.right-col{
		min-height:auto  !important;
	}
	.u-icon-disabled{
		background-color: $primary;
	}
	.u-number-input{
		background:var(--background-three-color) !important;
		border:1px solid var(--border-color) ;
		border-radius: 8rpx;
		color: var(--reversal-color) !important;
	}
}
</style>
